package com.briup.web.servlet;
import java.io.IOException;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.briup.bean.User;
import com.briup.common.exception.UserException;
import com.briup.service.IUserService;
import com.briup.service.impl.IUserServiceImpl;


@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		String zip=request.getParameter("zip");
		String address=request.getParameter("address");
		String phone=request.getParameter("telephone");
		String email=request.getParameter("email");

		User user=new User();
		user.setAddress(address);
		user.setDob(new Date());
		user.setEmail(email);
		user.setPassword(password);
		user.setPhone(phone);
		user.setUsername(name);
		user.setZip(zip);
		
		System.out.println(user);
		IUserService service=new IUserServiceImpl();
		try {
			service.registerUser(user);
		} catch (UserException e) {
			e.printStackTrace();
		}
		response.setContentType("text/jsp");
		String site = new String("login.jsp");
		response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
		response.setHeader("Location", site);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

